<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="商家名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(3)">查询</el-button>
        <el-button @click="getDataList(1)">只看已审核评论</el-button>
        <el-button @click="getDataList(0)">只看未审核评论</el-button>
        <el-button @click="getDataList(2)">只看审核未通过评论</el-button>
        <el-button v-if="isAuth('diancan:shopfoodaudit:update')" type="danger" @click="PassAuditBatch()" :disabled="dataListSelections.length <= 0">批量审核</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="merchant_name"
        header-align="center"
        align="center"
        label="店铺名称">
      </el-table-column>
      <el-table-column
        prop="comment_info"
        header-align="center"
        align="center"
        label="评论">
      </el-table-column>
      <el-table-column
        prop="score"
        header-align="center"
        align="center"
        label="分数">
      </el-table-column>
      <el-table-column
	    prop="comment_time"
	    header-align="center"
	    align="center"
	    label="评论时间">
      </el-table-column>
      <el-table-column prop="audit_state"   label="当前状态" show-overflow-tooltip align="center">
        <template slot-scope="scope">
            <p v-if="scope.row.audit_state==0">未审核</p>
            <p v-if="scope.row.audit_state==1">已审核</p>
            <p v-if="scope.row.audit_state==2">审核未通过</p>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" v-if="scope.row.audit_state==0 || scope.row.audit_state==2" @click="PassAudit(scope.row.id)">审核通过</el-button>
          <el-button type="text" size="small" v-if="scope.row.audit_state==0 || scope.row.audit_state==1" @click="NotPassAudit(scope.row.id)">不通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
    },
    activated () {
      this.getDataList()
    },
    watch: {
      shopcar (value) {
        if (value) {
          this.$store.commit('common/updateIsOpen')
          this.pageIndex = 1
          this.getDataList(this.$store.state.common.updateId)
        }
      }
    },
    computed: {
      // 计算属性
      shopcar () {
        return this.$store.state.common.isOpen
      }
    },
    methods: {
      // 获取数据列表
      getDataList (id) {
        var name = localStorage.getItem('name')
        var ids = localStorage.getItem('ids')
        if (name !== this.dataForm.key) {
          this.pageIndex = 1
          localStorage.setItem('name', this.dataForm.key)
        }
        if (id != null && parseInt(ids) !== id) {
          this.pageIndex = 1
          localStorage.setItem('ids', id)
        }
        this.$store.commit('common/updateUpdateId', id)
        this.$http({
          url: this.$http.adornUrl('/diancan/commoncomment/listPage'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'state': id,
            'redis': 'Aduit' + this.dataForm.key + this.pageIndex + this.pageSize + id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList(this.$store.state.common.updateId)
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList(this.$store.state.common.updateId)
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 批量审核通过
      PassAuditBatch (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对编号为:[${ids.join(',')}]进行['批量审核通过']操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/diancan/commoncomment/passAuditBatch'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.$store.state.common.updateId)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 审核通过
      PassAudit (id) {
        this.$http({
          url: this.$http.adornUrl('/diancan/commoncomment/auditComment'),
          method: 'get',
          params: this.$http.adornParams({
            'id': id,
            'auditState': 1
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.pageIndex = 1
                this.getDataList(this.$store.state.common.updateId)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 不通过
      NotPassAudit (id) {
        this.$http({
          url: this.$http.adornUrl('/diancan/commoncomment/AuditComment'),
          method: 'get',
          params: this.$http.adornParams({
            'id': id,
            'auditState': 2
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.pageIndex = 1
                this.getDataList(this.$store.state.common.updateId)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
